import axios from 'axios'
import {  Message } from 'element-ui'
import store from '@/store'
// import { getToken } from '@/utils/auth'
// import router from '@/router'

// create an axios instance
const service = axios.create({
  baseURL:'http://180.76.118.202:8080/'   
  // baseURL: 'http://192.168.1.134:8080/' // 基础路径
  //baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // baseURL: process.env.api,
  // withCredentials: true, // send cookies when cross-domain requests
  // timeout: 60000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    // config.headers['Content-Type'] = 'application/json'
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key 约定为Authorization
      // please modify it according to the actual situation
      
      // config.headers['Authorization'] = getToken()
      //暂时写死
      // config.headers['Authorization'] = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfYWNfdGsiOiIwNDgyNDIwQzNCRDYwMjBFQjZFMTcyODQyRDQxRTE5MjAxN0Y0RjQ3Mzc5MzlGQzRCOUIwQzA5MEVCMUJBN0U2RDg0MzNGOUY0RkVDNERFM0YzQzk2MThBOTgxQkFENUNCM0Y1QzhEMEFGNEQ3Q0MwRkU4MTAyQTc4QTI2MDNDRjU4RkVDRTQ2RkQ3RDhGRUQ5RDg1QjEwRkM0RUZBMzNEODgwRUMxNzcwQzI1OTY0QjlBQzYwMkM2Q0ZCOEJGMDY5RTAzMDZGQ0JENEQ3RUNCRTc0Q0VBODA3MkNFNTIzNEE0IiwiaWF0IjoxNTc3OTQ5NTE2fQ.KtKoLTW5UxE9zHac8ugpAxKTzqoS0PxR5SJOt1zhZ7s"
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
   
    return response
  },
  error => {
    switch (error.response&&error.response.data&&error.response.data.code){
      
      case 401:
          console.log('无权限')
          Message({
            message: error.response.data.msg,
            type: 'error',
            duration: 5000
          })
      break
      case 403:
       console.log('返回403,token过期')
       Message({
        message: error.response.data.msg,
        type: 'error',
        duration: 5000
      })
      break 
    }
    return Promise.reject(error.response)
  }
  
)

export default service
